package com.example.flowabledemo;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class FlowableMultiTest3 {

    @Autowired
    ProcessEngineConfiguration processEngineConfiguration;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    IdentityService identityService;

    private final String processDefinitionKey ="flow_mutil_patren2";

    private final String processDefinitionKeyNoGateWay = "multi_noGateway";
    private final String processDefinitionKeyNormal = "multi_one";


    @Test
    void deployFlow(){

        Deployment deploy;
        deploy = repositoryService.createDeployment()
                // .addClasspathResource("process/multi/multi.bpmn20.xml")
                // .addClasspathResource("process/multi/multi_noGateway.bpmn20.xml")
                .addClasspathResource("process/multi/flow_mutil_patren2.bpmn20.xml")
                .key(processDefinitionKey)
                .name("会签测试")
                .deploy();
        System.out.println(deploy.getId());
    }

    @Test
    void startProcess(){
        //首先通过流程标识  找到pdf
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(processDefinitionKey).singleResult();
        //启动流程
        Map<String,Object> map =new HashMap<>();
        map.put("userList", Arrays.asList("张三","李四","王五","赵六"));
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(), map);
        System.out.println("启动流程实例=======>"+processInstance.getId());
    }


    @Test
    void completeTask1(){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(processDefinitionKey).singleResult();
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstance.getId())
                .taskCandidateOrAssigned("张三")
                .singleResult();
        Map<String,Object> map =new HashMap<>();
        map.put("result", 1);
        taskService.complete(task.getId(),map );
    }

    @Test
    void completeTask2(){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(processDefinitionKey).singleResult();
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstance.getId())
                .taskCandidateOrAssigned("李四")
                .singleResult();
        Map<String,Object> map =new HashMap<>();
        map.put("result", 1);
        taskService.complete(task.getId(),map );
    }

    @Test
    void completeTask3(){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(processDefinitionKey).singleResult();
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstance.getId())
                .taskCandidateOrAssigned("王五")
                .singleResult();
        Map<String,Object> map =new HashMap<>();
        map.put("result", 0);
        taskService.complete(task.getId(),map );
    }


    @Test
    void completeTask4(){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(processDefinitionKey).singleResult();
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstance.getId())
                .taskCandidateOrAssigned("张三")
                .singleResult();
        Map<String,Object> map =new HashMap<>();
        map.put("result", 1);
        taskService.complete(task.getId(),map );
    }


    @Test
    void getModel(){
        String prodfId ="flow_mutil_patren2:1:e8141dd4-1bce-11ef-b8e2-a08069abebfe";
        repositoryService.getBpmnModel(prodfId);;
    }
}
